Word Text Alignment contains four styles: align left, align center, align right, and justify. All these kinds are frequently used when we set our word text styles. With the text alignment, our document text looks very clear and nice. Sometimes, people are specially asked to align the articles or papers in a certain alignment style. Thus, text alignment can be indispensable for word users. So I decide to write this post to meet people’s need. Before I show the method to alignment text in word with C#, VB.NET. I suggest use a word component Spire.Doc to help me finish this task.
Spire.Doc, can be used to perform a wide range of Word Document processing tasks directly without using Microsoft Word. It supports C#, VB.NET, ASP, .NET MVC and Silverlight. It has been widely used by the most Fortune 500 corporations. If you want to give it a try, please
Spire.Doc, can be used to perform a wide range of Word Document processing tasks directly without using Microsoft Word. It supports C#, VB.NET, ASP, .NET MVC and Silverlight. It has been widely used by the most Fortune 500 corporations. If you want to give it a try, please
Freely Download Spire.Doc
How to align text in word document with C#,VB.NET
The following procedure clearly shows you how to easily align text in word with C#,VB.NET by using Spire.Doc. Please follow it step by step:
Step1. Create a Project.
1. Create a new project in Visual Studio, no need of Forms.
2. Set the target Framework to be .NET Framework 4.
3. Add Spire.Doc DLL as reference.
4. Add Spire.Doc and Spire.Doc. Documents as using at the top of the method. Please see:
C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace Word_Alignment
{
class Program
{
static void Main(string[] args)
{
…
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace Word_Alignment
Class Program
Private Shared Sub Main(args As String())
…
End Sub
End Class
End Namespace
Step2. Create a Word Document.
C# Code:
//Create a document
Document document = new Document();
//Create a section
Section section = document.AddSection();
VB.NET Code:
'Create a document
Dim document As New Document()
'Create a section
Dim section As Section = document.AddSection()
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace Word_Alignment
Class Program
Private Shared Sub Main(args As String())
…
End Sub
End Class
End Namespace
Step2. Create a Word Document.
C# Code:
//Create a document
Document document = new Document();
//Create a section
Section section = document.AddSection();
VB.NET Code:
'Create a document
Dim document As New Document()
'Create a section
Dim section As Section = document.AddSection()
Step3. Align text in word
C# Code:
//Alignment left
Paragraph paragraph1 = section.AddParagraph();
paragraph1.AppendText(" Text Alignment- Left");
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left;
//Alignment center
Paragraph paragraph2 = section.AddParagraph();
paragraph2.AppendText(" Text Alignment- Center");
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Alignment right
Paragraph paragraph3 = section.AddParagraph();
paragraph3.AppendText(" Text Alignment- Right");
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Alignment justify
Paragraph paragraph4 = section.AddParagraph();
paragraph4.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. The demo Text demonstrates the horizontal alignment Justify");
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Justify;
VB.NET Code:
‘Alignment left
Dim paragraph1 As Paragraph = section.AddParagraph()
paragraph1.AppendText(" Text Alignment- Left")
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left
'Alignment center
Dim paragraph2 As Paragraph = section.AddParagraph()
paragraph2.AppendText(" Text Alignment- Center")
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center
'Alignment right
Dim paragraph3 As Paragraph = section.AddParagraph()
paragraph3.AppendText(" Text Alignment- Right")
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Right
'Alignment justify
Dim paragraph4 As Paragraph = section.AddParagraph()
paragraph4.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. The demo Text demonstrates the horizontal alignment Justify")
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Justify
Step4.Save and launch the file.
C# Code:
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launch the file.
System.Diagnostics.Process.Start("Sample.doc");
VB.NET Code:
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc)
//Launch the file.
System.Diagnostics.Process.Start("Sample.doc")
Preview
C# Code:
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launch the file.
System.Diagnostics.Process.Start("Sample.doc");
VB.NET Code:
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc)
//Launch the file.
System.Diagnostics.Process.Start("Sample.doc")
Preview
Text Alignment is just one of the Word Text Styles. Besides, there are many other styles in word, such as word color, word font, word indent and so on. Please see more articles about word text styles.